package com.edu.exam.enums;

/**
 * 基础四舍五入规则枚举
 *
 * @author System
 * @version 1.0.0
 */
public enum FoundationRoundingRule {
    ROUND_HALF_UP("ROUND_HALF_UP", "四舍五入"),
    ROUND_UP("ROUND_UP", "向上取整"),
    ROUND_DOWN("ROUND_DOWN", "向下取整"),
    ROUND_HALF_EVEN("ROUND_HALF_EVEN", "银行家舍入法"),
    ROUND_CEILING("ROUND_CEILING", "向正无穷取整"),
    ROUND_FLOOR("ROUND_FLOOR", "向负无穷取整"),
    ROUND_HALF_DOWN("ROUND_HALF_DOWN", "五舍六入");

    private final String code;
    private final String description;

    FoundationRoundingRule(String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 根据代码获取枚举值
     *
     * @param code 代码
     * @return 枚举值
     * @throws IllegalArgumentException 如果代码无效
     */
    public static FoundationRoundingRule fromCode(String code) {
        if (code == null) {
            throw new IllegalArgumentException("Code cannot be null");
        }
        for (FoundationRoundingRule rule : values()) {
            if (rule.getCode().equals(code)) {
                return rule;
            }
        }
        throw new IllegalArgumentException("Unknown code: " + code);
    }

    /**
     * 根据描述获取枚举值
     *
     * @param description 描述
     * @return 枚举值
     * @throws IllegalArgumentException 如果描述无效
     */
    public static FoundationRoundingRule fromDescription(String description) {
        if (description == null) {
            throw new IllegalArgumentException("Description cannot be null");
        }
        for (FoundationRoundingRule rule : values()) {
            if (rule.getDescription().equals(description)) {
                return rule;
            }
        }
        throw new IllegalArgumentException("Unknown description: " + description);
    }

    @Override
    public String toString() {
        return description;
    }
}